package com.crb.crm.manage.message.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Package: com.gulimall.thrid.party.msg
 * Description：消息服務對外api
 * Author: huangjian
 * Date:  2020/6/18 14:58
 * Modified By:
 */
@Component
public class MessagSenderContext {

    private SMGSender sender = null;

    private final Map<SMGChannel, SMGSender> ctx = new HashMap<>();

    @Autowired
    public MessagSenderContext(List<SMGSender> smgSenders) {
        smgSenders.stream().forEach(s -> {
            ctx.put(s.support(), s);
        });
    }

    public MessagSenderContext channel(SMGChannel smgChannel) {
        sender = ctx.get(smgChannel);
        Assert.notNull(sender,"請選擇正確的渠道");
        return this;
    }

    public SMGSender template(SAGTemplate template) {
        Assert.notNull(sender,"請選擇正確的渠道");
        sender.setTemplate(template);
        return sender;
    }
}
